package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.config.Constant;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-01-07
 * Time: 11:21
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注册功能
     */
    @RequestMapping("/reg")
    public Object register(String username, String password){
        //1.非空校验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
           return AjaxResult.fail(-1, "非法请求");
        }

        //2.判断用户名是否已经存在
        User user = userService.getUserByUserName(username);
        if(user!=null){
            return AjaxResult.fail(-1,"用户名已存在!");
        }

        //对密码进行加密
        String finalPassword = SecurityUtil.encrypt(password);

        //3.添加用户
        if(userService.register(username, finalPassword)){
            return AjaxResult.success(1);
        } else {
            return AjaxResult.fail(-1, "注册失败!");
        }
    }

    @RequestMapping("/login")
    public Object login(String username, String password, HttpServletRequest request){
        //1.非空校验
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return AjaxResult.fail(-1, "非法请求");
        }

        //2.判断用户名是否存在
        User user = userService.getUserByUserName(username);

        if(user==null){
            return AjaxResult.fail(-1,"用户名不存在,请先注册!");
        } else {
            //3.验证密码的正确性
            if( SecurityUtil.decrypt(password, user.getPassword())){
                //正确,将当前用户的信息存入到seesion中
                HttpSession session = request.getSession();
                session.setAttribute(Constant.SESSION_USERINFO_KEY, user);
                return AjaxResult.success("登录成功",1);
            } else {
                return AjaxResult.fail(-1,"用户名或密码错误!");
            }
        }
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public boolean layout(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if( session != null && session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null){
            //移除当前session中登录的用户
            session.removeAttribute(Constant.SESSION_USERINFO_KEY);
        }
        return true;

    }

    /**
     * 获取个人信息
     * @param request
     * @return
     */
    @RequestMapping("/myinfo")
    public User getmyinfo(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if( session!=null && session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null){
            User user = (User)session.getAttribute(Constant.SESSION_USERINFO_KEY);
            return user;
        }
        return null;
    }

    /**
     * 根据id获取到个人信息
     */
    @RequestMapping("/myinfobyuid")
     public User getUerById(Integer uid ){
        return userService.getUerById(uid);
     }
}
